package cn.jd.org.controller;

import cn.jd.basic.anno.LogAnno;
import cn.jd.basic.config.BussinessException;
import cn.jd.basic.util.AjaxResult;
import cn.jd.basic.util.PageList;
import cn.jd.org.domain.Systemdictionarydetail;
import cn.jd.org.domain.Systemdictionarytype;
import cn.jd.org.domain.Employee;
import cn.jd.org.query.SystemdictionarytypeQuery;
import cn.jd.org.service.SystemdictionarytypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/systype")
@Api(value = "部门的API",description="部门相关的CRUD功能")
@LogAnno
public class SystemdictionarytypeController {
    @Autowired
    private SystemdictionarytypeService systemdictionarytypeService;
    @PutMapping
    public AjaxResult addSystemdictionarytype(@RequestBody Systemdictionarytype systemdictionarytype) {
        try {
            if(systemdictionarytype.getId() == null){
                systemdictionarytypeService.save(systemdictionarytype);
                return AjaxResult.me().success();
            }else {
                systemdictionarytypeService.update(systemdictionarytype);
                return AjaxResult.me().success();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }
   @DeleteMapping("/{id}")
    @LogAnno(method = "deleteSystemdictionarytype", methodDes = "删除的方法")
    public  AjaxResult deleteSystemdictionarytype(@PathVariable("id") Long id){

        try {
            systemdictionarytypeService.removeAllType(id);
            return AjaxResult.me().success();
        }catch (BussinessException bu){
            bu.printStackTrace();
            System.out.println("++++++++++++++++++++++++++6666666666666666666");
            return AjaxResult.me().fail(bu.getMessage());
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }

    @GetMapping
    @ApiOperation(value = "查询所有数据" )
    public List<Systemdictionarytype> findall(){
        return systemdictionarytypeService.findAll();
    }
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询一条数据" )
    public  Systemdictionarytype findall(@ApiParam(value="传入long类型的数据",required = true) @PathVariable("id") Long id){
        return systemdictionarytypeService.findById(id);
    }
    @PostMapping
    @LogAnno(method = "findall", methodDes = "分页查询的方法")
    public PageList<Systemdictionarytype> findall(@RequestBody SystemdictionarytypeQuery query){

        return systemdictionarytypeService.queryByPage(query);
    }
    // 批量删除
    @PatchMapping
    public  AjaxResult deleteAll(@RequestBody List<Long> ids){

        try {
            systemdictionarytypeService.deleteByAll(ids);
            return AjaxResult.me().success();
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.me().fail();
    }
    //systypeson
    @GetMapping("/systypeson/{id}")
    @ApiOperation(value = "根据id查询一条数据" )
    public  List<Systemdictionarydetail> findSystemdictionarydetail(@ApiParam(value="传入long类型的数据",required = true) @PathVariable("id") Long id){
        return systemdictionarytypeService.findSystemdictionarydetail(id);
    }
}
